package com.itheima.reggie.controller;

import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.List;
import java.util.Map;

@RestController//将返回的数据结构转换为json
public class EmployeeController {
    @Autowired//依赖注入
    private EmployeeService employeeService;
    @Autowired
    HttpSession session;

    @PostMapping("/employee/login")//处理post请求   @RequestBody//将json转为Java对象
    public ResultInfo login(@RequestBody Map<String, String> param) {
//请求参数
        String username = param.get("username");
        String password = param.get("password");
        //调用service参数
        ResultInfo resultInfo = employeeService.login(username, password);
        //3.登陆成功将员工信息存储到session
        if (resultInfo.getCode() == 1) {
            Employee employee = (Employee) resultInfo.getData();
            session.setAttribute(Constant.SESSION_EMPLOYEE, employee);
        }
        return resultInfo;
    }

    @PostMapping("/employee/logout")
    //退出
    public ResultInfo logout() {
        //1.清除session
        session.invalidate();
        //返回结果
        return ResultInfo.success(null);
    }

    //员工列表
    @GetMapping("/employee/find")
    public ResultInfo findList(String name) {
        //接收请求参数
        //2.调用service查询员工列表
        List<Employee> list = employeeService.findList(name);
        //返回结果
        return ResultInfo.success(list);
    }

    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee) {
        //1.接收请求参数
        //2.调用service新增
        employeeService.save(employee);
        //3.返回结果
        return ResultInfo.success(null);
    }

    //回显员工
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable Long id) {//1.接收参数
        //2.调用service
        Employee employee = employeeService.findById(id);
        //3.返回结果
        return ResultInfo.success(employee);
    }
    //修改员工
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee) {
        //1,接收参数
        //2.调用service修改
        employeeService.update(employee);
        //3.返回结果
        return ResultInfo.success(null);
    }
}

































































